<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ page import="com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadqInventoryFlowOrder"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>千禧宝贝连锁店管理信息系统</title>
<%@ include file="../../common/Style.jsp"%>
<script type="text/javascript" src="<%=request.getContextPath()%>/conf_files/js/inventory/inventory.js?v=1.11"></script>
    <style>
        #chat-box{
            height: 560px; width: 96%; margin-bottom: 5px; overflow:auto; border: 1px solid #999;
        }
    </style>
<script>
var dataGrid ;
$(document).ready(function(){
	parent.$.messager.progress('close'); 	
	loadResult();
	var orderId = $("#orderId").val();
	$('#dataGrid').edatagrid({
			url : 'headqInventoryFlowJSONAction!selectOrderProducts',
			queryParams: {
				'formBean.flowOrder.id':orderId
			},
            fitColumns: true,
            fit: true,
            singleSelect: true,
            rowStyler: function(index,row){
    			var style = "";
    			if (row.quantity > 3)
    				style += 'color:red;';
    			return style;
    		},
    		editable: false,
            columns: [[
                {field: 'id', title: '编号', width: 30},
                {field: 'pbId', title: '条码ID', width: 70, hidden:true},
                {field: 'barcode', title: '条码', width: 70, editor: false},
                {field: 'productCode', title: '货号', width: 70, editor: false},
                {field: 'colorName', title: '颜色', width: 60, editor: false},
                {field: 'brand', title: '品牌', width: 70, editor: false},
                {field: 'categoryLevel', title: '产品线', width: 50, editor: false},
                {field: 'category', title: '货品类别', width: 60, editor: false},
                {field: 'year', title: '年份', width: 40, editor: false,
                	formatter : function(value, row, index) {	
                		return row.year + row.quarter;
                	}
                },
                {field: 'unit', title: '单位', width: 30, editor: false},
                {field: 'salesPrice', title: '批发单价', width: 40, editor: false},
                {field: 'quantity', title: '实际数量', width: 40, editor: false},
                {field: 'totalWholePrice', title: '批发总价', width: 50, editor: false},
                {field: 'inventoryQ', title: '电脑库存', width: 40, editor: false},
                {field: 'action1', title: '纠正量', width: 30, editor: false,
                	formatter : function(value, row, index) {	
                		return row.quantity - row.inventoryQ;
                	}},
                {field: 'action', title: '图片', width: 30,
        			formatter : function(value, row, index) {
        				var str = '';     				
        				str += $.formatString('<a href="#" onclick="viewImgP(\'{0}\');"><img border="0" src="{1}" /></a>', row.barcode,'<%=request.getContextPath()%>/conf_files/easyUI/themes/icons/img3.png');        			       				
        				return str;
        			}},
        		{field: 'scanNums', title: '扫描次数', width: 30, editor: false},
        		{field: 'comment', title: '备注', width: 40, editor: 'text'}
            ]]});
	
	//计算合计
	 var totalQuantity = $("#totalQuantity").val();
	 var totalInventoryQ = $("#totalInventoryQ").val();
	 var totalQuantityDiff = $("#totalQuantityDiff").val();
	 $("#sumQuantity").val(totalQuantity);
	 $("#inventoryQuantity").val(totalInventoryQ);
	 $("#correctQuantity").val(totalQuantityDiff);
	 
});


/**
 * 库存单：下载单据
 */
function downloadOrder(){
    document.headqInventoryFlowForm2.action="headqInventoryFlowJSPAction!downloadFlowOrder";  								
    document.headqInventoryFlowForm2.submit();
}

/**
 * 库存单校验
 */
function validateInvenFlowForm(){
	var totalQ = $("#sumQuantity").val();
	var file = $("#inventory").textbox('getValue');
	if (totalQ == 0 && file == ""){
		$.messager.alert('错误', '请输入货品或者导入盘点文件后再操作单据', 'warning');
		return false;
	}
    return true;				
}

/**
 * 库存单：存入草稿
 */
function saveDraft(){
	if (validateInvenFlowForm()){
		$.messager.confirm('功能确认','你确认提交仓库订单', function(r){
			if (r){
				$.messager.progress({
					text : '数据处理中，此功能耗时较长.请稍后....'
				});					 
				   saveBeforeLoad();
				   document.headqInventoryFlowForm.action = "action/headqInventoryFlowJSPAction!saveInventoryToDraft";
				   document.headqInventoryFlowForm.submit();	            
			}else{
				$.messager.progress('close');
			}
			});
	}
}
 
 /**
  * 库存单：刷新库存
  */
 function calInventoryOrder(){
		if (validateInvenFlowForm()){
			saveBeforeLoad();
			document.headqInventoryFlowForm.action = "action/headqInventoryFlowJSPAction!refreshInventoryOrder";
			document.headqInventoryFlowForm.submit();	 				            						
		}
 }

/**
 * 库存单：删除单据
 */

 function deleteOrder(){
		$.messager.confirm('功能确认','你确定删除此单据吗?', function(r){
			if (r){
				   var url = "<%=request.getContextPath()%>/action/headqInventoryFlowJSONAction!deleteOrder";
			 	   var params=$("#headqInventoryFlowForm").serialize();  
			 	   $.post(url,params, deleteBackProcess,"json");	
			}
		});
}
 
/**
 * 库存单：删除单据回调
 */
 function deleteBackProcess(data){
 	$.messager.progress('close');
     var response = data;
 	var returnCode = response.returnCode;
 	if (returnCode != SUCCESS)
 		$.messager.alert('错误', response.message, 'error');
 	else {
 		$.messager.alert({
 			title: '成功',
 			closable: false,
 			msg: '单据删除成功',
 			fn: function(){
 				parent.closeTab();
 			}
 		});
     }		
 }
 

/**
 * 库存单：一键纠正库存 -获取年份季度信息
 */
 function oneKeyToAdjustInventory(){
	 var file = $("#inventory").textbox('getValue');
		if (file != ""){
		  var msg = "如果有盘点文件，请先上传文件然后保存到草稿中.";
		  $.messager.alert('错误', msg, 'warning');
		}else{
			if(validateInvenFlowForm()) {
				oneKeyConfirm();
			}					
		}	 
}
	
/**
 * 库存单：一键纠正库存	
 */
function oneKeyConfirm(){
	$.messager.confirm('功能确认','你确定上传货品的实际库存已经正确.一键纠正库存将根据当前实际库存，纠正单据中物品电脑库存?', function(r){
		  if(r){
			  $.messager.progress({
					text : '数据处理中，此功能耗时较长.请稍后....'
				});	
		 	   
		 	 	saveBeforeLoad();
				document.headqInventoryFlowForm.action = "action/headqInventoryFlowJSPAction!oneKeyAdjustInventory";
				document.headqInventoryFlowForm.submit();
		  }else{
			  $.messager.progress('close');
		  }	  	
	 }); 					  
} 


/**
 * 库存单：库存扎帐 
 */
function oneKeyToClearAndAdjustInventory(){
	var file = $("#inventory").textbox('getValue');
	if (file != ""){
	  var msg = "如果有盘点文件，请先上传文件然后保存到草稿中.";
	  $.messager.alert('错误', msg, 'warning');
	} else{
		if (validateInvenFlowForm()){
			$.messager.progress({
				text : '数据处理中，此功能耗时较长.请稍后....'
			});	
			saveBeforeLoad();
	        var params = $("#headqInventoryFlowForm").serialize(); 
		    $.post("headqInventoryFlowJSONAction!getYearQuarterInFlowOrder",params, oneKeyToClearAndAdjustInventoryBK,"json");
	    }
	}
}
//库存扎帐回调
function oneKeyToClearAndAdjustInventoryBK(data){
	parent.$.messager.progress('close'); 
	var response = data.response;
	var returnCode = response.returnCode;
	if (returnCode == SUCCESS){
		var confirmMsg ="请确定此单据中只包含了如下季度货品: " + response.returnValue + "\n\n" +
						"此功能将使用单据中的实际库存完全取代连锁店的电脑库存.请确认这张单据里面的季度是你这次盘点所包含的季度吗?";
		
		$.messager.confirm('功能确认',confirmMsg, function(r){
			if (r){				
				$.messager.progress({
					text : '数据处理中，此功能耗时较长.请稍后....'
				});				   
			    saveBeforeLoad();
				document.headqInventoryFlowForm.action = "action/headqInventoryFlowJSPAction!oneKeyClearAndAdjustInventory";
				document.headqInventoryFlowForm.submit();
			}else{
				$.messager.progress('close');
			}
				
		});	

	} else {
		$.messager.alert('错误', '获取货品季度出现出错，请稍后再试', 'error');
	}
} 

// 生成最终报表
function genInventoryReport(){
	var msg = "生成库存报表后，将不能再修改，是否继续?";

	if (validateInvenFlowForm()){
		genInventoryConfirm();
	}
}

/**
 * 库存单：生产最终报表回调
 */
function genInventoryConfirm(){
	$.messager.confirm('功能确认','生成库存报表后，将不能再修改，是否继续??', function(r){
		  if(r){
			  $.messager.progress({
					text : '数据处理中，请稍后....'
				});	
			    saveBeforeLoad();
				document.headqInventoryFlowForm.action = "action/headqInventoryFlowJSPAction!genInventoryReport";
				document.headqInventoryFlowForm.submit();
		  }else{
			  $.messager.progress('close');
		  }
	 }); 		  
}

//下载条码
function getBarcodeFile(){

	document.headqInventoryFlowForm.action = "action/headqInventoryFlowJSPAction!getBarcodeFile";
	document.headqInventoryFlowForm.submit();	
}

/**
 * 合并单据
 */
function mergeDocument(){
	var documents = $("#documentIds").textbox('getValue');
	var storeId = $("#storeId").combobox('getValue');
	if(documents != null && documents !=""){
		var url = "action/headqInventoryFlowJSPAction!mergeDocument?formBean.documentIds=" + documents +"&formBean.flowOrder.storeId=" + storeId;
		document.headqInventoryFlowForm.action = url;
		document.headqInventoryFlowForm.submit();	
	}else{
		$.messager.alert('提示', '合并单据号为空，请输入单据号！', 'warning');
	}

}


</script>
</head>
<body>

<s:form action="/action/inventoryFlowAction!saveToDraftN" method="POST" name="headqInventoryFlowForm" enctype="multipart/form-data" id="headqInventoryFlowForm" theme="simple">
	<s:hidden name="formBean.flowOrder.type"/>  
	<s:hidden name="formBean.flowOrder.id"/>   
	<s:hidden name="formBean.flowOrder.status"/> 
	<input type= "hidden" id="totalQuantity"  value='<s:property value="formBean.flowOrder.totalQuantity"/>'></input>
	<input type= "hidden" id="totalInventoryQ"  value='<s:property value="formBean.flowOrder.totalInventoryQ"/>'></input>
	<input type= "hidden" id="totalQuantityDiff"  value='<s:property value="formBean.flowOrder.totalQuantityDiff"/>'></input>
	<input type= "hidden" id="jsons"  name="formBean.jsons"></input>
	<input type="hidden" id="result" name="formBean.responseMsg"></input>
	<input type= "hidden" id="orderId" value='<s:property value="formBean.flowOrder.id"/>'></input>
	    <table width="95%" align="center"  class="OuterTable">
	    <tr><td>
	    
	      <!--  <div class="errorAndmes"><s:actionerror cssStyle="color:red"/><s:actionmessage cssStyle="color:blue"/></div>-->
	      <%@ include file="../common/JRComponent.jsp"%>
			 <table width="100%" border="0">
			    <tr>
			       <td height="50" colspan="7">
				   	 <table width="100%" border="0">
				       <tr class="PBAOuterTableTitale">
				         <td height="35" colspan="5">
				        	总部库存单据：<s:property value="formBean.flowOrder.statusS" />
				        	
				        	<span style="position:relative;left:85%;">单据号：
				         		<s:if test="formBean.flowOrder.id != 0">
				        		   	<s:property value="formBean.flowOrder.id"/>
				        		  </s:if>
				         	</span>	
				         </td>				         
			           	 
			           </tr>
				      <tr class="InnerTableContent">
				       	<td width="22%" height="40"><%@ include
												file="../common/SearchProductCodeInput.jsp"%>
								<s:select name="formBean.fullOrSingle" id="fullOrSingle"   list="#{'0':'单件','1':'齐手'}" listKey="key" listValue="value" style="width:65px" cssClass="easyui-combobox" data-options="editable:false" ></s:select>				
						</td>
				         <td width="10%"><strong>创建人:</strong>&nbsp &nbsp<s:property value="formBean.flowOrder.creator.name"/></td>
				         <td width="20%"><strong>单据日期:</strong>&nbsp &nbsp<s:property value="formBean.flowOrder.orderDate"/></td>
				        				        
				         <td  width="17%"><strong>仓库信息：</strong>
				         		<s:select name="formBean.flowOrder.storeId"  cssClass="easyui-combobox" style="width:140px"  size="1" id="storeId" list="uiBean.stores" listKey="id" listValue="name"  data-options="editable:false"/>
				         </td>
				         <td><strong>数量：</strong>
				         	<input class="easyui-textbox" id="correctNums" data-options="prompt:'请输入纠正后数量',searcher:insert" style="width:130px">
				         <a href="javascript:void(0)" class="easyui-linkbutton c6" onclick="updateRow()" style="width:80px; height: 25px;">修改行</a><a href="javascript:void(0)" class="easyui-linkbutton c7" onclick="deleteRow()" style="width:80px;height: 25px;">删除行</a></td>
				       </tr>

				     </table></td>
			    </tr>			    			    
			    <tr>
			      <td colspan="7">
			            <!-- table to display the product information -->
								<div id="chat-box">
									<table id="dataGrid" style="width: 1700px;">
									</table>
								</div>
							</td>
			    </tr>
			    <tr class="InnerTableContent">
			      <td colspan="7">
			            
						<table width="100%" align="left" border="0">
						<tr class="InnerTableContent">
															
								<td></td>
								<td></td>
								<td></td>
								<td><strong>盘点总数量：</strong><input type="button" id="sumQuantity"  style="height:25px;width:75px;color:red;text-align:center;border-radius:10%; font-size: 12px;"  disabled="disabled"/>
									<strong>库存总数量：</strong><input type="button" id="inventoryQuantity"  style="height:25px;width:75px;color:red;text-align:center;border-radius:10%; font-size: 12px;"  disabled="disabled"/>
									<strong>纠正：</strong><input type="button" id="correctQuantity"  style="height:25px;width:75px;color:red;text-align:center;border-radius:10%; font-size: 12px;"  disabled="disabled"/>
								</td>
							</tr>						
						<tr class="InnerTableContent">
															
								<td width="5%" align="left" height="40"><strong>文件上传:</strong></td>
								<td width="15%"><input class="easyui-filebox" id="inventory"  name="formBean.inventory" data-options="prompt:'请上传新增条码文件',buttonText: '条码文件',buttonAlign: 'left'" style="width:90%"></input>
								<td width="5%" align="right"><strong>备注:</strong></td>
								<td width="50%"><s:textfield cssClass="easyui-textbox" data-options="prompt:'请输入单据备注信息！'" style="width:50%"
												name="formBean.flowOrder.comment" size="50" /></td>
							</tr>

 						<tr class="InnerTableContent">
						    <td  align='left'  height="45"><strong>合并单据:</strong></td>

						    <td colspan="3" align='left'>
						    	<s:textfield cssClass="easyui-textbox" id="documentIds" style="width:40%"  data-options="prompt:'请输入需要和并的单据的单据号(*注：多单据号用英文“,” 分隔开，例如：102,103,104)'" size="50" />						    	
						    	<s:if test="(#session.LOGIN_USER.containFunction('headqInventoryFlowJSPAction!mergeDocument')) ">	
						    		<a href="#" class="easyui-linkbutton c3" style="width:100px" onclick="mergeDocument()">合并单据</a>
						    	</s:if>
						    </td>
						  </tr>						  			 
						  <tr class="InnerTableContent">
						    <td  align='left'><strong>选择操作:</strong></td>

						    <td colspan="3" align='left'>
						    			<s:if test="(#session.LOGIN_USER.containFunction('headqInventoryFlowJSPAction!saveInventoryToDraft')) ">											
											<a href="#" class="easyui-linkbutton c8" data-options="iconCls:'icon-add'" onclick="saveDraft();" >存入草稿</a>
										</s:if>
										<s:if test="(#session.LOGIN_USER.containFunction('headqInventoryFlowJSPAction!saveInventoryToDraft')) ">
											<a id="refresh" href="#" class="easyui-linkbutton c8" data-options="iconCls:'icon-reload'" onclick="calInventoryOrder();" >刷新电脑库存</a>
										</s:if>
										<s:if test="formBean.flowOrder.id !=0">
											<a href="#" class="easyui-linkbutton c8" data-options="iconCls:'icon-no'" onclick="deleteOrder();" >删除单据</a>
										</s:if> 
										<s:if test="formBean.flowOrder.id > 0">
											<a href="#" class="easyui-linkbutton c8" data-options="iconCls:'icon-print'"onclick="downloadOrder();" >下载单据</a>
										</s:if> 
										<s:if test="(#session.LOGIN_USER.containFunction('headqInventoryFlowJSPAction!oneKeyAdjustInventory')) ">
											<a href="#" class="easyui-linkbutton c8" data-options="iconCls:'icon-edit'" onclick="oneKeyToAdjustInventory();">一键纠正库存</a>
										</s:if>
										<s:if test="(#session.LOGIN_USER.containFunction('headqInventoryFlowJSPAction!oneKeyClearAndAdjustInventory')) ">
											<a href="#" class="easyui-linkbutton c8" data-options="iconCls:'icon-sum'" onclick="oneKeyToClearAndAdjustInventory();">库存扎帐</a> 
										</s:if>
										<s:if test="(#session.LOGIN_USER.containFunction('headqInventoryFlowJSPAction!genInventoryReport')) ">
											<a href="#" class="easyui-linkbutton c8" data-options="iconCls:'icon-large-chart'" onclick="genInventoryReport();">生成最终报表</a>
										</s:if>
										<s:if test="(#session.LOGIN_USER.containFunction('headqInventoryFlowJSPAction!getBarcodeFile')) ">
											<a href="#" class="easyui-linkbutton c8" data-options="iconCls:'icon-print'" onclick="getBarcodeFile();">下载条码</a>																												 				      					
						    			</s:if>						    			
						    </td>
					      </tr>
					    </table>
				  </td>
			    </tr>
			  </table>
	   </td></tr>
	 </table> 
	 </s:form>
	<s:form action="" method="POST" name="headqInventoryFlowForm2" id="headqInventoryFlowForm2" theme="simple">
		<s:hidden name="formBean.flowOrder.id" />
	</s:form>
</body>
</html>